
***method mix 
use "${PilotPublicData}/hgopy_pilot_public.dta", clear
*fix up the method in mind list so it includes the current users 
*set method in mind = yes for people who want to continue with their method and need to renew
replace method_in_mind=1 if want_to_do==1 
replace method_in_mind=0 if want_to_do==2 & mind==0 & method_experience==1 & discuss_non_larc==1 & missing(method_in_mind) & missing(method_mind_list)
replace method_in_mind=0 if want_to_do==2 & mind==0 & method_experience==2 & missing(discuss_non_larc) & missing(method_in_mind) & missing(method_mind_list)
tab method_in_mind , m
*which method did they have in mind
replace method_mind_list=current_method_list if want_to_do==1 & missing(method_mind_list) & method_in_mind==1
replace method_mind_list=0 if want_to_do==2 & missing(method_mind_list) & method_in_mind==0
tab method_mind_list
recode method_mind_list (1/2=1 "LARC") (3/99=2 "SARC") (0=.) , gen(method_type)
tab method_type //, m
*recode into a set of vars with the same values 
label list current_method_list
recode current_method_list (0=0 "None") (1=1 "IUD") (2=2 "Implant") (3=3 "Pill") (4=4 "Injectable") (6=5 "LAM") (5=6 "Male/Female condoms") (7=7 "Traditional or other"), gen(method_cur)
label var method_cur "Current method"
label list method_mind_list
recode method_mind_list (0=0 "None") (1=1 "IUD") (2=2 "Implant") (3=3 "Pill") (4=4 "Injectable") (6=5 "LAM") (5 8=6 "Male/Female condoms") (7 9 10 99=7 "Traditional or other"), gen(method_min)
label var method_min "Desired method" 
label list adopted_method
recode adopted_method  (0=0 "None") (1=1 "IUD") (2=2 "Implant") (3=3 "Pill") (4=4 "Injectable") (6=5 "LAM") (7=6 "Male/Female condoms") (8=7 "Traditional or other"), gen(method_ado)
label list adopted_other
gen adopted_lam = adopted_lam1
replace method_ado=6 if adopted_other==1
replace method_ado=7 if inlist(adopted_other,2,99)
replace method_ado=5 if adopted_lam==1 & !inlist(adopted_method,1,2,3,4) 
label var method_ado "Adopted method" 
*check out 
tab method_cur 
tab method_min if method_min!=0
tab method_ado
*extract proportions 
capture matrix drop P
qui foreach m of varlist method_cur method_min method_ado { 
	capture matrix drop M_`m'p
	capture matrix drop M_`m'n
	*calculate 
	prop `m' 	
	*freq 
	forvalues i=1/8 {
		matrix M_`m'n = nullmat(M_`m'n) \ (e(freq)[1,`i'])
	}
	*proportion
	forvalues i=1/8 {
		matrix M_`m'p = nullmat(M_`m'p) \ (e(b)[1,`i'])
	}
	matrix P = nullmat(P) ,  M_`m'n , M_`m'p
	*get total N for later
	su `m'
	local n`m'=r(N)
}
matrix P = nullmat(P) \ (`nmethod_cur',1,`nmethod_min',1,`nmethod_ado',1)
matrix list P 
*make a table 
clear 
svmat P 
*local with first column names 
gen A=""
order A, first 
local firstcol = `"  "None" "IUD" "Implant" "Pill" "Injectable" "LAM" "Male/Female condoms" "Traditional or other" "Total" "' 
local i=0
foreach j of local firstcol {
	local ++i 
	replace A="`j'" if _n==`i'
}
*add LARC and SARC and Other totals
gen n=_n
local n = _N+3
set obs `n'
replace A="LARC" if _n==_N-2
replace n=1.5 if A=="LARC"
replace A="SARC" if _n==_N-1
replace n=3.5 if A=="SARC"
replace A="Other" if _n==_N
replace n=5.5 if A=="Other"
gen type="larc" if inlist(A,"IUD","Implant","LARC")
replace type="sarc" if inlist(A,"Pill","Injectable","SARC")
replace type="other" if inlist(A,"Male/Female condoms","Traditional or other","LAM","Other")
forvalues i=1/6 {
	bys type: egen p`i'=total(P`i')
}
forvalues i=1/6 {
	replace P`i'=p`i' if inlist(A,"LARC","SARC","Other")
} 
sort n 
drop p? type n
foreach v of varlist P2 P4 P6 {
	gen x`v'=string(`v'*100,"%9.2f")
	order x`v', after(`v')
}
drop P2 P4 P6  

replace A = "~~" + A if inlist(_n, 3,4,6,7,9,10,11)
#delimit ;
texsave_custom using "${OutTexTab}/TabS2-method-mix.tex" , replace nonames  hlines(11) frag nofix
	align(" p{0.3\textwidth}*{6}{>{\centering\arraybackslash}p{0.075\textwidth}} ")
	headerlines(" & (1) & (2) & (3) & (4) & (5) & (6) "
				" & \multicolumn{2}{c}{\textbf{Current method}} & \multicolumn{2}{c}{\textbf{Method in mind}} & \multicolumn{2}{c}{\textbf{Method adopted}} "
				" \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){2-3} & N & \% & N & \% & N & \% " )	; 
#delimit cr

